﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMP.ERP.Model;
using CMP.ERP.BLL;
using System.Data;

namespace CMP.ERP.Admin.Controllers
{
    public class OrderController : Controller
    {
        /// <summary>
        /// 实例化对象
        /// </summary>
        ShipmentsBLL bll = new ShipmentsBLL();
        ExpressCompanyBLL ebll = new ExpressCompanyBLL();
        WareHoseBLL wbll = new WareHoseBLL();
        //
        // GET: /Order/
        /// <summary>
        /// 显示订单信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Order_Show()
        {
            List<ExpressCompany> list = new List<ExpressCompany>();
            DataTable data = ebll.ExpressCompanyShow();
            foreach (DataRow item in data.Rows)
            {
                ExpressCompany e = new ExpressCompany();
                e.Express_Company_Id = Convert.ToInt32(item["Express_Company_Id"]);
                e.Express_Company_Name = item["Express_Company_Name"].ToString();
                e.Express_Company_Address = item["Express_Company_Address"].ToString();
                e.Express_Company_Phone = item["Express_Company_Phone"].ToString();
                e.Express_Company_Charges = item["Express_Company_Charges"].ToString();
                list.Add(e);
            }
            ViewBag.Express_Company_Id = new SelectList(list, "Express_Company_Id", "Express_Company_Name");
            List<WareHose> plist = new List<WareHose>();
            DataTable dataer = wbll.WareHoseShow();
            foreach (DataRow item in dataer.Rows)
            {
                WareHose e = new WareHose();
                e.WareHose_Id = Convert.ToInt32(item[0]);
                e.WareHose_Name = item[1].ToString();
                e.WareHose_Address = item[2].ToString();
                e.WareHose_People = item[3].ToString();
                e.WareHose_Phone = item[4].ToString();
                e.WareHose_Remark = item[5].ToString();
                e.Commodity_Url = item[6].ToString();
                plist.Add(e);
            }
            ViewBag.WareHose_Id = new SelectList(plist, "WareHose_Id", "WareHose_Name");
            return View(bll.ShowShipments());
        }
        /// <summary>
        /// 根据年月日查询下单信息
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        /// <param name="Day"></param>
        /// <returns></returns>
        public ActionResult SelShipment(string  StartTime, string End_Time, string Shipments_Per, string Shipments_Phone)
        {
            List<ExpressCompany> list1 = new List<ExpressCompany>();
            DataTable data1 = ebll.ExpressCompanyShow();
            foreach (DataRow item in data1.Rows)
            {
                ExpressCompany e = new ExpressCompany();
                e.Express_Company_Id = Convert.ToInt32(item["Express_Company_Id"]);
                e.Express_Company_Name = item["Express_Company_Name"].ToString();
                e.Express_Company_Address = item["Express_Company_Address"].ToString();
                e.Express_Company_Phone = item["Express_Company_Phone"].ToString();
                e.Express_Company_Charges = item["Express_Company_Charges"].ToString();
                list1.Add(e);
            }
            ViewBag.Express_Company_Id = new SelectList(list1, "Express_Company_Id", "Express_Company_Name");
            List<WareHose> plist = new List<WareHose>();
            DataTable dataer = wbll.WareHoseShow();
            foreach (DataRow item in dataer.Rows)
            {
                WareHose e = new WareHose();
                e.WareHose_Id = Convert.ToInt32(item[0]);
                e.WareHose_Name = item[1].ToString();
                e.WareHose_Address = item[2].ToString();
                e.WareHose_People = item[3].ToString();
                e.WareHose_Phone = item[4].ToString();
                e.WareHose_Remark = item[5].ToString();
                e.Commodity_Url = item[6].ToString();
                plist.Add(e);
            }
            ViewBag.WareHose_Id = new SelectList(plist, "WareHose_Id", "WareHose_Name");


            List<Shipments> list = new List<Shipments>();
            DataTable data = bll.SelShipments(StartTime, End_Time, Shipments_Per, Shipments_Phone);
            foreach (DataRow item in data.Rows)
            {
                Shipments e = new Shipments();
                e.Shipments_Id = Convert.ToInt32(item["Shipments_Id"]);
                e.Shipments_data = item["Shipments_data"].ToString();
                e.Shipments_Address = item["Shipments_Address"].ToString();
                e.Shipments_Per = item["Shipments_Per"].ToString();
                e.Shipments_Phone = item["Shipments_Phone"].ToString();
                e.Shipments_State = item["Shipments_State"].ToString();
                e.Express_Company_Name = item["Express_Company_Name"].ToString();
                e.WareHose_Name = item["WareHose_Name"].ToString();
                e.Shipments_SumMoney = Convert.ToInt32(item["Shipments_SumMoney"]);
                e.purchaser = item["purchaser"].ToString();
                list.Add(e);
            }
            return View("Order_Show", list);
        }
        /// <summary>
        /// 显示商品数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult show()
        {
            //实例化商品对象
            CMP.ERP.BLL.CommodityBLL bll = new CommodityBLL();
            ViewBag.CommodityType_Id = new SelectList(bll.GetCommodityTypeAll(), "CommodityType_Id", "CommodityType_Name");
            return Json(bll.GetCommodityAll(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult add(string Shipments_Per, string Shipments_Phone, int Express_Company_Id, int WareHose_Id, string Shipments_Address, string purchaser) 
        {

            Shipments s = new Shipments();
            s.Shipments_data = DateTime.Now.ToString();
            s.Shipments_Per = Shipments_Per;
            s.Shipments_Phone = Shipments_Phone;
            s.Express_Company_Id = Express_Company_Id;
            s.Shipments_Address = Shipments_Address;
            s.purchaser = purchaser;
            s.WareHose_Id = WareHose_Id;
            string[] num = Request["ShipmentMate_num"].ToString().Split(',');
            string[] price = Request["Commodity_price"].ToString().Split(',');
            string[] commodityid = Request["Commodity_Id"].ToString().Split(',');
            List<ShipmentMate> list = new List<ShipmentMate>();
            for (int i = 0; i < num.Length; i++)
            {
                ShipmentMate m = new ShipmentMate();
                m.Commodity_Id = Convert.ToInt32(commodityid[i]);
                m.ShipmentMate_Num = Convert.ToInt32(num[i]);
                m.Commodity_Price = Convert.ToInt32(price[i]);
          
                list.Add(m);
            }
            List<int> listnow = bll.AddShipments(s, list).ToList();
            var content = 0;
            foreach (int item in listnow)
            {
                if (item == 1)
                {
                    content = 1;
                }
                else
                {
                    content = 0;
                    continue;
                }
            }
            if (content == 1)
            {
                return Content("True");
            }
            else
            {
                return Content("Fales");
            }
        }
        /// <summary>
        /// 查看订单详情信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DetailsShipmentsShow(int id)
        {
            Session["id"] = Convert.ToInt32(id);
            List<DetailsShipments> list = new List<DetailsShipments>();
            DataTable data = bll.DetailsShow(id);
            foreach (DataRow item in data.Rows)
            {
                DetailsShipments c = new DetailsShipments();
                c.ShipmentMate_Id = Convert.ToInt32(item["ShipmentMate_Id"]);
                c.Shipments_Id = Convert.ToInt32(item["Shipments_Id"]);
                c.Commodity_Name = item["Commodity_Name"].ToString();
                c.Commodity_number = Convert.ToInt32(item["Commodity_number"]);
                c.Commodity_Weight = Convert.ToInt32(item["Commodity_Weight"]);
                c.Commodity_Remark = item["Commodity_Remark"].ToString();
                c.Commodity_Price = Convert.ToInt32(item["Commodity_Price"]);
                c.CommodityType_Name = item["CommodityType_Name"].ToString();
                c.ShipmentMate_Num =  Convert.ToInt32(item["ShipmentMate_Num"]);   
                list.Add(c);
            }
            return View(list);           
        }
        /// <summary>
        /// 订单主表显示数据信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DetailsShipmentsShow2(int id)
        {
            List<Shipments> list = new List<Shipments>();
            DataTable data2 = bll.ShowXiangqing1(id);
            foreach (DataRow item in data2.Rows)
            {
                Shipments s2 = new Shipments();
                s2.Shipments_Id = Convert.ToInt32(item["Shipments_Id"]);
                s2.Shipments_data = item["Shipments_data"].ToString();
                s2.Shipments_Per = item["Shipments_Per"].ToString();
                s2.Shipments_Phone = item["Shipments_Phone"].ToString();
                s2.Shipments_State = item["Shipments_State"].ToString();
                s2.Express_Company_Name = item["Express_Company_Name"].ToString();
                s2.WareHose_Name = item["WareHose_Name"].ToString();
                s2.Shipments_SumMoney = Convert.ToInt32(item["Shipments_SumMoney"]);
                list.Add(s2);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult update(string pidtr)
        {
            string[] pidtre = pidtr.ToString().Split(',');
            List<ShipmentMate> list = new List<ShipmentMate>();
            int Shipments_Id = 0;
            for (int i = 0; i < pidtre.Length; i++)
            {
                ShipmentMate s = new ShipmentMate();
                foreach (var item in bll.huoquiry(Convert.ToInt32(pidtre[i])))
                {
                    s.Commodity_Id = item.Commodity_Id;
                    Shipments_Id = item.Shipments_Id;
                    s.ShipmentMate_Num = item.ShipmentMate_Num;
                }

                list.Add(s);
            }
            List<int> listnow = bll.Update(Shipments_Id, list).ToList();
            int content = 0;
            foreach (int item in listnow)
            {
                if (item == 1)
                {
                    content = 1;
                }
                else
                {
                    content = 0;
                    continue;
                }
            }
            if (content == 1)
            {
                return Content("True");
            }
            else
            {
                return Content("Fales");
            }


        }
    }
}
